package cn.com.eado.crm.v.fragment


import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import cn.com.eado.crm.R
import cn.com.eado.crm.databinding.FragmentPersonBaoBeiBinding
import cn.com.eado.crm.model.LoadStatus
import cn.com.eado.crm.model.RetryCallback
import cn.com.eado.crm.v.adapter.RVCompanyAdapter
import cn.com.eado.crm.v.weiget.AutoSpinner
import cn.com.eado.crm.vm.CompanyViewModel
import cn.com.eado.preado.app.utils.autoCleared
import com.getbase.floatingactionbutton.FloatingActionButton
import kotlinx.android.synthetic.main.fragment_person_bao_bei.*

/**
 * A simple [Fragment] subclass.
 */
class PersonBaoBeiFragment : BaseBindingFragment<FragmentPersonBaoBeiBinding>(R.layout.fragment_person_bao_bei) {

    private var vm by autoCleared<CompanyViewModel>()
    private var adapter by autoCleared<RVCompanyAdapter>()
    private lateinit var title: String

    override fun initTheFragment() {
        initView()
        initViewModel()
        initBinding()
        loadData()
    }

    private fun initView() {
        initTitleBar()
        initOptionMenu()
        initRecyclerView()
    }

    private fun initTitleBar() {
        title = arguments?.getString("name")?:"XXX管理"
        titleBar4.setTitle(title)
    }

    private fun initOptionMenu(){
        fab_menu3.findViewById<FloatingActionButton>(R.id.fab_1).setOnClickListener {
            val layout = initAddTrack()
            val dialog = AlertDialog.Builder(activity!!)
                .setTitle("添加跟踪")
                .setView(layout)
                .setPositiveButton("提交", null)//结合下面的dialog.getButton，保证点击确定不关闭dialog
                .setNegativeButton("取消"){ dialog, pos -> }
                .show()
            dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener{//必须在show()之后
                if (checkTrack(layout)) dialog.dismiss()
            }
        }

        fab_menu3.findViewById<FloatingActionButton>(R.id.fab_2).setOnClickListener {
            val layout = initAddLoss()
            val dialog = AlertDialog.Builder(activity!!)
                .setTitle("流失")
                .setView(layout)
                .setPositiveButton("提交", null)
                .setNegativeButton("取消"){ dialog, pos -> }
                .show()
            dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener{
                if (checkLoss(layout)) dialog.dismiss()
            }
        }

        fab_menu3.findViewById<FloatingActionButton>(R.id.fab_3).setOnClickListener {
            val bundle = Bundle()
            bundle.putString("name", title)
//            Navigation.findNavController(view!!).navigate(R.id.action_personBaoBeiFragment_to_searchFragment, bundle)
        }
    }

    private fun initAddTrack(): View {
        val layout = View.inflate(activity, R.layout.add_company, null)
        val inputSpinner1 = layout.findViewById<AutoSpinner>(R.id.inputSpinner1)
        inputSpinner1.setList(listOf<String>("aaa","bbb","ccc"))  //for test
        return layout
    }

    private fun checkTrack(layout: View): Boolean {
        return true
    }

    private fun initAddLoss(): View {
        val layout = View.inflate(activity, R.layout.add_company, null)
        val inputSpinner1 = layout.findViewById<AutoSpinner>(R.id.inputSpinner1)
        inputSpinner1.setList(listOf<String>("aaa","bbb","ccc"))  //for test
        return layout
    }

    private fun checkLoss(layout: View): Boolean {
        return true
    }

    private fun initRecyclerView() {
        recyclerView3.layoutManager = LinearLayoutManager(activity)
        val adapter = RVCompanyAdapter({ view, pos, bean ->  //click
            //
        },{ pos, bean ->  //long click
            //
        })
        recyclerView3.adapter = adapter
        this.adapter = adapter
    }

    private fun initViewModel(){
        vm = ViewModelProviders.of(this).get(CompanyViewModel::class.java)
//        vm.result.observe(this, Observer {
//            if (it.status == LoadStatus.SUCCESS){
////                Log.e("tag", "it=${it.data}")
//            }
//        })
    }

    private fun initBinding(){
        binding.lifecycleOwner = viewLifecycleOwner
//        binding.result = vm.result
        binding.callback = object : RetryCallback {
            override fun retry() {
                vm.refresh()
            }
        }
    }

    private fun loadData(){
        vm.setLoadCompany(true)
    }


}
